<%@ page contentType="text/html;charset=UTF-8" %>
<%@ page import="com.jiadakeji.ppm.modules.sys.utils.*" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>

<head>
    <title>发票预览</title>
    <meta name="decorator" content="default"/>
    <script src="${ctxStatic}/jquery-form/jquery.form.js" type="text/javascript"></script>
    <link href="${ctxStatic}/css/font-awesome.min.css?v=4.4.0" rel="stylesheet">
    <link href="${ctxStatic}/css/animate.min.css" rel="stylesheet">
    <link href="${ctxStatic}/css/common.css" rel="stylesheet">
    <link href="${ctxStatic}/css/style.min.css?v=4.0.0" rel="stylesheet">
    <OBJECT ID=sk
            CLASSID="clsid:003BD8F2-A6C3-48EF-9B72-ECFD8FC4D49F"
            codebase="NISEC_SKSCX.ocx#version=1,0,0,1" width=0 height=0>
    </OBJECT>
    <script type="text/javascript">
        $(document).ready(function() {
            $("#invioceForm").height($(window).height()-35);
            //$("#name").focus();
            $("#inputForm").validate({
                submitHandler: function(form){

                },
                errorContainer: "#messageBox",
                errorPlacement: function(error, element) {
                    $("#messageBox").text("输入有误，请先更正。");
                    if (element.is(":checkbox")||element.is(":radio")||element.parent().is(".input-append")){
                        error.appendTo(element.parent().parent());
                    } else {
                        error.insertAfter(element);
                    }
                }
            });
        });
    </script>
    <style>
        body{ background: #FFF;}
        .fixed {
            table-layout: auto;
            width: 70%;
            text-align: center;
        }

        .collapse {
            border-collapse: collapse;
            border: 1px solid black;
            overflow: scroll;
        }

        caption {
            font-size: 1.5em;
            font-weight: bolder;
            padding: 0 10px;
        }

        .table-top {
            font-size: .7em;
            font-weight: normal;
            width: 100%;
            display: flex;
            display: -webkit-flex;
            justify-content: space-between;
            -webkit-justify-content: space-between;
            margin-bottom: 0em;
        }
       .table-top li{ display: inline-block}
        table th {
            padding: 5px;
        }

        table td {
            padding: 5px;
            height: 1.5em;
        }
        .text-left{
            text-align: left;
            width: 100px;

        }
    </style>
</head>

<body >

    <form:form id="invioceForm" modelAttribute="fillInvioce" action="${ctx}/invioce/filling"  method="post" class="form-horizontal" style="overflow: auto;width:98%;">
        <form:hidden path="content.kpzdbs" value="${counter}"/>
        <form:hidden path="content.fpqqlsh" value="${feeReceive.id}"/>
        <form:hidden path="content.kplx" value="0"/>
        <form:hidden path="content.qdbz" value="0"/>
        <form:hidden path="content.zsfs" value="0"/>
        <input type="hidden" id="chargeType" name="chargeType" value="${chargeType}"/>  <%--用于发票打印后的操作员处理--%>
        <table border="1" class="collapse fixed" align="center">
            <caption>
                <span style="margin-bottom: 10px;display: inline-block;">发票信息填写</span>
                <div class="table-top">
                    <li><label >发票类型</label>
                        <form:radiobuttons path="content.fplxdm" items="${fns:getDictList('fplsdm')}" class="required" onclick="changeInvioceType()" itemLabel="label" itemValue="value"/></li>
                    <li style="margin-right: 8%;"><label >特殊票种</label>
                        <form:radiobuttons path="content.tspz" items="${fns:getDictList('tspz')}" class="required"  itemLabel="label" itemValue="value"/></li>
                </div>
            </caption>
            <tbody>
            <tr>
                <td rowspan="4" style="width: 20px;">购买方</td>
                <td class="text-left">名&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;称：</td>
                <td class="text-left" colspan="10"><form:input path="content.ghdwmc" value="${feeReceive.customer.name}" class="input-xlarge"/>  </td>
                <td rowspan="4" style="width: 20px;">密码区</td>
                <td rowspan="4" colspan="10"></td>
            </tr>
            <tr>
                <td class="text-left">纳税人识别号：</td>
                <td class="text-left" colspan="10"><form:input path="content.ghdwsbh" value="" class="input-xlarge" /></td>
            </tr>
            <tr>
                <td class="text-left">地 &nbsp;址、电&nbsp;话：</td>
                <td class="text-left"  colspan="10"><form:input path="content.ghdwdzdh" value="" class="input-xlarge"/></td>
            </tr>
            <tr>
                <td class="text-left">开户行及账号：</td>
                <td class="text-left" colspan="10"><form:input path="content.ghdwyhzh" value="" class="input-xlarge"/></td>
            </tr>
            <tr>
                <td colspan="8">货物或应税劳务、服务名称</td>
                <td colspan="2">规格型号</td>
                <td colspan="1">单位</td>
                <td colspan="2">数 量</td>
                <td colspan="2">单 价</td>
                <td colspan="3">金 额</td>
                <td>税率</td>
                <td colspan="3">税 额</td>
            </tr>
            <c:forEach items="${goodsList}" var="goods" varStatus="count" >
                <tr>
                    <form:hidden path="content.salesGoods.group[${count.index}].xh" value="${goods.xh}"/>
                    <form:hidden path="content.salesGoods.group[${count.index}].fphxz" value="0"/>
                    <form:hidden path="content.salesGoods.group[${count.index}].spsm"/>
                    <td colspan="8"><form:input path="content.salesGoods.group[${count.index}].spmc" value="${goods.spmc}" class="input-medium"/></td>
                    <td colspan="2"><form:input path="content.salesGoods.group[${count.index}].ggxh" value="${goods.ggxh}" class="input-medium"/></td>
                    <td><form:input path="content.salesGoods.group[${count.index}].dw" value="${goods.dw}" class="input-medium"/></td>
                    <td colspan="2"><form:input path="content.salesGoods.group[${count.index}].spsl" value="${goods.spsl}" class="input-medium"/></td>
                    <td colspan="2"><form:input path="content.salesGoods.group[${count.index}].dj" value="${goods.dj}" class="input-medium"/></td>
                    <td colspan="3"><form:input path="content.salesGoods.group[${count.index}].je" value="${goods.je}" class="input-medium"/></td>
                    <td><form:input path="content.salesGoods.group[${count.index}].sl" value='${goods.sl eq "0.0"? "0":goods.sl}' class="input-medium"/></td>
                    <td colspan="3"><form:input path="content.salesGoods.group[${count.index}].se" value="${goods.se}" class="input-medium"/></td>
                    <form:hidden path="content.salesGoods.group[${count.index}].hsbz" value="0"/>
                    <form:hidden path="content.salesGoods.group[${count.index}].spbm" value="${goods.spbm}"/>
                    <form:hidden path="content.salesGoods.group[${count.index}].zxbm"/>
                    <form:hidden path="content.salesGoods.group[${count.index}].yhzcbs" value="${goods.yhzcbs}"/>
                    <form:hidden path="content.salesGoods.group[${count.index}].lslbs" value="${goods.lslbs}"/>
                    <form:hidden path="content.salesGoods.group[${count.index}].zzstsgl" value="${goods.zzstsgl}"/>
                </tr>
            </c:forEach>
            <tr>
                <td colspan="8">合计</td>
                <td colspan="2"></td>
                <td></td>
                <td colspan="2"></td>
                <td colspan="2"></td>
                <td colspan="3"><form:input path="content.hjje" value=" ${preContent.hjje}" class="input-medium"/></td>
                <td></td>
                <td colspan="3"> <form:input path="content.hjse" value=" ${preContent.hjse} " class="input-medium"/></td>
            </tr>
            <tr>
                <td colspan="8">价税合计（大写）</td>
                <td style="text-align: left" colspan="11"><input name="moneySum" value=" ${moneySum}" class="input-medium"/></td>
                <td style="border-right: none;border-left: none;width: 35px;">(小写)</td>
                <td style="border-left: none;" colspan="2"><form:input path="content.jshj" value=" ${preContent.jshj}" class="input-small"/></td>
            </tr>
            </tbody>
            <tfoot>
            <tr>
                <td rowspan="4" style="width: 20px;">销售方</td>
                <td class="text-left">名&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;称：</td>
                <td class="text-left" colspan="10"><form:input path="content.xhdwmc" value=" ${map.xhdwmc} " class="input-xlarge"/></td>
                <td rowspan="4" style="width: 20px;">备注</td>
                <td rowspan="4" colspan="10" class="text-left" ><form:textarea path="content.bz" value="${preContent.bz}" class="input-medium" rows="4"  maxlength="255" /></td>
            </tr>
            <tr>
                <td class="text-left">纳税人识别号：</td>
                <td class="text-left" colspan="10"><form:input path="content.xhdwsbh" value="${map.xhdwsbh}" class="input-xlarge"/></td>
            </tr>
            <tr>
                <td class="text-left">地 &nbsp;址、电&nbsp;话：</td>
                <td class="text-left"  colspan="10"><form:input path="content.xhdwdzdh" value="${map.xhdwdzdh}" class="input-xlarge"/></td>
            </tr>
            <tr>
                <td class="text-left">开户行及账号：</td>
                <td class="text-left" colspan="10"> <form:input path="content.xhdwyhzh" value="${map.xhdwyhzh}" class="input-xlarge"/></td>
            </tr>
            </tfoot>
        </table>
        <div style="width: 80%;">
            <div class="table-top" style="font-size: .9em;margin-top: .8em;margin-left: 13%;">
                <li>收&nbsp;&nbsp;款&nbsp;&nbsp;人:<form:input path="content.skr" value="${preContent.skr}" /></li>
                <li>复&nbsp;&nbsp;核:<form:input path="content.fhr"  value="${preContent.fhr}" /></li>
                <li>开&nbsp;&nbsp;票&nbsp;&nbsp;人:<form:input path="content.kpr" value="${preContent.kpr}" class="input-medium"/></li>
                <li style="margin-right: 8%;">销&nbsp;&nbsp;售&nbsp;&nbsp;方:&nbsp;&nbsp;（章）</li>
            </div>
        </div>
        <form:hidden path="content.kce"/>
        <form:hidden path="content.tzdbh"/>
        <form:hidden path="content.yfpdm"/>
        <form:hidden path="content.yfphm"/>
        <div style="width: 100%; text-align: center; margin: 35px 0px 25px 0px;">
            <button type="button" onclick="fillingInvioce()" class="btn btn-warning" >发票开具</button>
            <input id="btnCancel" class="btn" type="button" value="返 回" onclick="history.go(-1)"/>
        </div>

    </form:form>

    <script type="text/javascript">

        function changeInvioceType() {
            var type = $("input[name='content\\.fplxdm']:checked").val()
            console.log(type);
            if(type == "004"){

                $("content\\.ghdwmc").attr("src","xx");
                $("content\\.ghdwsbh").attr("required","true");
                $("content\\.ghdwdzdh").attr("required","true");
                $("content\\.ghdwyhzh").attr("required","true");
            }if( type == "007"){
                $("content\\.ghdwmc").removeAttr("class","required");
                $("content\\.ghdwsbh").removeAttr("class","required");
                $("content\\.ghdwdzdh").removeAttr("class","required");
                $("content\\.ghdwyhzh").removeAttr("class","required");
            }

        }



        function fillingInvioce() {
            var queryString = $('#invioceForm').formSerialize();
            SetParameter();
            SetMargin();
            $.ajax({
                async : false,
                type : "POST",  //提交方式
                url : "${ctx}/invioce/filling",//路径
                data : queryString,
                success : function(result) {//返回数据根据结果进行相应的处

                    invioceFIll(result);

                }

            })
            updateReceive();


        }

        function SetMargin(){

            $.ajax({
                async : false,
                type : "POST",  //提交方式
                url : "${ctx}/invioce/getMarginProperty",//路径
                data : {
                    "fplxdm":$("input[name='content\\.fplxdm']:checked").val()
                },
                success : function(result) {//返回数据根据结果进行相应的处
                    marginSet(result);
                }

            })
            var sInputInfo = "<?xml version=\"1.0\" encoding=\"gbk\"?>\n" +
                "<business id=\"20003\" comment=\"页边距设置\">\n" +
                "<body yylxdm=\"1\">\n" +
                "<fplxdm>007</fplxdm>\n" +
                "<top>0</top>\n" +
                "<left>0</left>\n" +
                "</body>\n" +
                "</business>\n";
//            alert(sInputInfo);

        }

        function marginSet(result){
            var sInputInfo = result;
            console.log(sInputInfo);
            try
            {
                ret = sk.Operate(sInputInfo);
                console.log(ret);
            }
            catch(e)
            {
                alert(e.message + ",margin errno:" + e.number);
            }
        }



        function SetParameter()
        {
            var sInputInfo =
                "<?xml version=\"1.0\" encoding=\"gbk\"?>\r\n" +
                "<business id=\"20001\" comment=\"参数设置\">\r\n" +
                "<body yylxdm=\"1\">\r\n" +
                "<servletip>124.225.17.145</servletip>\r\n" +
                "<servletport>8082</servletport>\r\n" +
                "<keypwd>88888888</keypwd>\r\n" +
                "</body>\r\n" +
                "</business>";
            try
            {
                ret = sk.Operate(sInputInfo);
                console.log(ret);
            }
            catch(e)
            {
                alert(e.message + ", setParam errno:" + e.number);
            }
        }

        function invioceFIll(result){
            var sInputInfo = result;
            console.log(sInputInfo);
            try
            {
            ret = sk.Operate(sInputInfo);
            console.log(ret);
            doPrinter(ret);
            }
            catch(e)
            {
                alert(e.message + ",invioceFILL errno:" + e.number);
            }
        }


        function doPrinter(requsetXml) {
            var xml = requsetXml;
            // alert(xml);
            var kpzdbs = $("#content\\.kpzdbs").val();
            $.ajax({
                contentType:'application/x-www-form-urlencoded;charset=gbk',
                type : "POST",  //提交方式
                url : "${ctx}/invioce/doPrinter",//路径
                data : {
                    "requestXml": xml,
                    "fplxdm":$("input[name='content\\.fplxdm']:checked").val(),
                    "kpzdbs": kpzdbs
                },
                success : function(result) {//返回数据根据结果进行相应的处理
                    alert("请核对发票代码、发票号码与纸质发票一致：发票代码"+result.fpdm+";发票号码"+result.fphm);
                    if(result.code == "0") {

                        printetInvioce(result.xml);
                    }else {
                        alert("发生错误了！！"+result.xml);
                    }

                }

            })



        }

        function printetInvioce(result){
            var sInputInfo = result;
            console.log(sInputInfo);
            try
            {
                ret = sk.Operate(sInputInfo);
                alert(ret);
            }
            catch(e)
            {
                alert(e.message + ",printeInvioce errno:" + e.number);
            }
        }

        function updateReceive() {
            var chargeType = $("#chargeType").val();
            var receiveId = $("#content\\.fpqqlsh").val();
            $.ajax({
                async : false,
                type : "POST",  //提交方式
                url : "${ctx}/invioce/updateReceive",//路径
                dataType:"json",
                data : {
                    "chargeType":chargeType,
                    "receiveId":receiveId
                },
                success : function(result) {//返回数据根据结果进行相应的处
                    console.log(result.message);
                }

            })

        }

    </script>
</body>




</html>